package com.leyou.page.mq;


import com.leyou.common.constants.MQConstants;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {

    @Autowired
    private PageService pageService;

    /**
     * 上架消息的处理
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.Queue.PAGE_ITEM_UP,declare = "true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME,
                    type = ExchangeTypes.TOPIC),key = MQConstants.RoutingKey.ITEM_UP_KEY))
    public void listenItemUp(Long spuId){
        if (spuId != null){
            //给商品生成静态页
            pageService.createItemHtml(spuId);
        }
    }

    /**
     * 下架消息的处理
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.Queue.PAGE_ITEM_DOWN,declare = "true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME,
                    type = ExchangeTypes.TOPIC),key = MQConstants.RoutingKey.ITEM_DOWN_KEY))
    public void listenItemDown(Long spuId){

        if (spuId != null){
            //删除商品静态页面
            pageService.deleteItemHtml(spuId);
        }
    }
}
